package com.os.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.os.crm.mapper.EnterpriseMapper;
import com.os.crm.pojo.Enterprise;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import com.os.util.page.PageUtils;
import entity.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class EnterpriseService {
    public static final int SAVE_TYPE_INSERT = 1;
    public static final int SAVE_TYPE_UPDATE = 2;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    /**
     * 采用分页插件进行分页
     * @param pageRequest
     * @param map
     * @return
     */
    public PageResult selectByPage(PageRequest pageRequest, Map map) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Enterprise> enterpriseList = enterpriseMapper.selectByParam(map);
        Tools.indexOrder(enterpriseList,"index");
        PageInfo pageInfo = new PageInfo<Enterprise>(enterpriseList);
        return PageUtils.getPageResult(pageRequest, pageInfo);
    }
    /**
     * 按ID查询企业信息
     * @param id
     * @return
     */
    public Enterprise selectById(int id){
        return enterpriseMapper.selectById(id);
    }

    /**
     * 新增企业信息
     * @param enterprise
     */
    public void insert(Enterprise enterprise){
        enterpriseMapper.insertSelective(enterprise);
    }
    /**
     * 修改企业信息
     * @param enterprise
     */
    public void update(Enterprise enterprise){
        enterpriseMapper.updateByPrimaryKeySelective(enterprise);
    }

    /**
     * 按ID删除企业信息
     * @param id
     */
    public void deleteById(int id){
        enterpriseMapper.deleteById(id);
    }

    /**
     * 保存企业信息
     * @param enterprise
     */
    public int save(Enterprise enterprise){
        Map map = new HashMap();
        List paramList = new ArrayList();
        if(enterprise.getEnt_comment()!=null){
            paramList.add(new Param("ent_comment", enterprise.getEnt_comment() + "", "String","="));
        }
        map.put("condstr", Tools.genCondStr("", paramList));
        List<Enterprise> enterpriseList = enterpriseMapper.selectByParam(map);
        if(CollectionUtils.isEmpty(enterpriseList)){
            enterprise.setCreate_time(Tools.getNowTime());
            insert(enterprise);
            return SAVE_TYPE_INSERT;
        }else{
            String businessNameOld = enterpriseList.get(0).getBusiness_name()==null?"":enterpriseList.get(0).getBusiness_name();
            enterprise.setCreate_time(enterpriseList.get(0).getCreate_time()==null? Tools.getNowTime():enterpriseList.get(0).getCreate_time() );
            enterprise.setEnt_id(enterpriseList.get(0).getEnt_id());
            enterprise.setUpdate_time(Tools.getNowTime());
            if(!businessNameOld.contains(enterprise.getBusiness_name())){
                String businessNameNew = businessNameOld+","+enterprise.getBusiness_name();
                enterprise.setBusiness_name(businessNameNew);
            }else{
                return SAVE_TYPE_UPDATE;
            }
            update(enterprise);
            return SAVE_TYPE_UPDATE;
        }
    }
}
